<?php
class Model_Member extends Zend_Db_Table_Row_Abstract
{
    const ROLE_GUEST            = 0;
    const ROLE_MEMBER           = 1;
    const ROLE_MODERATOR        = 2;
    const ROLE_ADMINISTRATOR    = 3;

    const STATUS_NOT_VERIFIED   = 0;
    const STATUS_ACTIVE         = 1;
    const STATUS_BANNED         = 2;
    
    const SEX_MALE              = 1;
    const SEX_FEMALE            = 2;

    public static $gender = array(
        self::SEX_MALE   => 'male',
        self::SEX_FEMALE => 'female'
    );

    public static $statuses = array(
        self::STATUS_NOT_VERIFIED => 'not_verified',
        self::STATUS_ACTIVE       => 'active',
        self::STATUS_BANNED       => 'banned'
    );

    public static $roles = array(
        self::ROLE_MEMBER           => 'member',
        self::ROLE_MODERATOR        => 'moderator',
        self::ROLE_ADMINISTRATOR    => 'admin'
    );

    /**
     * @param  string $columnName The column key.
     * @param  mixed  $value      The value for the property.
     * @return void
     */
    public function __set($columnName, $value)
    {
        switch ($columnName) {
            case 'password':
                if (!$value) {
                    return;
                }
                if (!$this->salt) {
                    parent::__set('salt', Ext_App::getSalt());
                }
                $value = $this->hashPassword($value);
                break;
        }
        parent::__set($columnName, $value);
    }

    /**
     * @param string $password
     * @return string
     */
    public function hashPassword($password)
    {
        return sha1($password . $this->salt);
    }

    /**
     * @return void
     */
    public function activate()
    {
        $this->verify_key = null;
        $this->status     = self::STATUS_ACTIVE;
        $this->updated_at = new Zend_Db_Expr('CURDATE()');
        $this->save();
    }

    /**
     * @return stdClass 
     */
    public function simplify()
    {
        $member = new stdClass();
        $member->id        = $this->id;
        $member->email     = $this->email;
        $member->firstname = $this->firstname;
        $member->lastname  = $this->lastname;
        $member->username  = $this->username;
        $member->role      = $this->role;
        $member->status    = $this->status;
        $member->gender    = $this->gender;
        return $member;
    }
}